﻿/*///////////////////////////////////////////////////////////
TodoItemList.as
Ben Smith
2009
///////////////////////////////////////////////////////////*/

package com.bsmith.app.model 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	/*===========================================================
	TodoItemList
	List of todo items
	===========================================================*/
	public class TodoItemList extends EventDispatcher
	{
		static public const EVENT_CHANGED:String = "til.chg";
		
		private var _sType:String = null;	//List type
		private var _aItems:Array = null;	//List of items
		
		/*-----------------------------------------------------------
		TodoItemList - ctor
		-----------------------------------------------------------*/
		public function TodoItemList(sType:String) 
		{
			_sType = sType;
			_aItems = [];
		}
		
		/*-----------------------------------------------------------
		deserialize
		Loads todo list from XML
		-----------------------------------------------------------*/
		public function deserialize(xml:XML)
		{
			for each (var xmlItem:XML in xml.*)
				addItem(TodoItem.fromXML(xmlItem));
		}
		
		/*-----------------------------------------------------------
		serialize
		Creates XML from item list
		-----------------------------------------------------------*/
		public function serialize():XML
		{
			var xml:XML = <List/>;
			xml.setName(_sType);
			for each (var item:TodoItem in _aItems)
				xml.appendChild(item.serialize());
			return xml;
		}
		
		/*-----------------------------------------------------------
		addItem
		Adds a new todo item to the list
		-----------------------------------------------------------*/
		public function addItem(item:TodoItem)
		{
			item.addEventListener(TodoItem.EVENT_CHANGED, onItemChanged, false, 0, true);
			_aItems.push(item);
			this.dispatchEvent(new TodoEvent(TodoEvent.NEW_TODO_ENTRY, { item:item } ));
			this.dispatchEvent(new Event(EVENT_CHANGED));
		}
		
		/*-----------------------------------------------------------
		reorderItems
		Reorders the todo items
		-----------------------------------------------------------*/
		public function reorderItems(items:Array, fireEvent:Boolean = true)
		{
			_aItems = items;
			if (fireEvent)
				this.dispatchEvent(new TodoEvent(TodoEvent.REORDER_TODO_ENTRIES, { items:getItems() } ));
			this.dispatchEvent(new Event(EVENT_CHANGED));
		}
		
		/*-----------------------------------------------------------
		removeItem
		Removes a todo item from the list
		-----------------------------------------------------------*/
		public function removeItem(index:int)
		{
			var item:TodoItem = _aItems[index];
			item.removeEventListener(TodoItem.EVENT_CHANGED, onItemChanged);
			_aItems.splice(index, 1);
			this.dispatchEvent(new TodoEvent(TodoEvent.REMOVE_TODO_ENTRY, { index:index } ));
			this.dispatchEvent(new Event(EVENT_CHANGED));
		}
		
		/*-----------------------------------------------------------
		onItemChanged
		If an item changes, dispatch a list changed event
		-----------------------------------------------------------*/
		private function onItemChanged(e:Event):void 
		{
			this.dispatchEvent(new Event(EVENT_CHANGED));
		}
		
		/*###########################################################
		GETTERS AND SETTERS
		###########################################################*/
		public function getType():String { return _sType; }
		public function getNumItems():int { return _aItems.length; }
		public function getItems():Array { return _aItems; }
	}
}